package impl;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;


import vo.TblCancel;
import vo.TblContract;

import amushen.base.BaseBO;

public class CancelBO extends BaseBO {
	public List queryCancelList(String number, String contractId) {
		List list = null;
		Session session = getSession();
		String hql = "from TblCancel";
		
		if (("".equals(number) || number == null) && ("".equals(contractId) || contractId == null)) {
		} else {
			// inner join TblContract as con where tc.contractId=con.id and  
			if ((!"".equals(number) && number != null)&&(!"".equals(contractId) && contractId != null)) {
				hql = hql + " as tc where tc.contract.contractNumber like '%" + contractId + "%' and tc.number like '%" + number + "%'";
			}else if (!"".equals(number) && number != null) {
				hql = hql + " tc where " + " tc.number like '%" + number + "%'";
			} else if (!"".equals(contractId) && contractId != null) {
				hql = hql + " as tc where tc.contract.contractNumber like '%" + contractId + "%'";
			}
		}
		list = pageQuery(session.createQuery(hql));
		
		//force load
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			TblCancel cancel = (TblCancel) iterator.next();
			if (cancel.getContract() != null) {
				cancel.getContract().getContractNumber();
			}
		}
		
		commit();
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			TblCancel cancel = (TblCancel) iterator.next();
			if (cancel.getContract() == null) {
				TblContract contract = new TblContract();
				cancel.setContract(contract);
			}
		}
		
		return list;
	}
	
	public int deleteCancel(String id) {
		TblCancel cancel = new TblCancel();
		cancel.setId(Integer.parseInt(id));
		return deleteVO(cancel);
	}
	
	public TblCancel queryOneCancel(String id) {
		Session session = getSession();
		TblCancel cancel = (TblCancel) session.get(TblCancel.class, new Integer(id));
		if (cancel.getContract()!=null) {
			cancel.getContract().getContractNumber();
		}
		commit();
		return cancel;
	}
	
	public void saveCancel(TblCancel cancel) {
		saveVo(cancel);
	}
}
